package com.example.kt_wear_app.presentation.utils

import android.content.Context
import android.content.SharedPreferences

object SharedPreferencesUtils {
    private const val DEFAULT_PREF_NAME = "default_shared_prefs"
    private lateinit var sharedPreferences: SharedPreferences

    fun init(context: Context, prefName: String = DEFAULT_PREF_NAME) {
        sharedPreferences = context.getSharedPreferences(prefName, Context.MODE_PRIVATE)
    }

    fun putBoolean(key: String, value: Boolean) {
        checkInitialization()
        sharedPreferences.edit().putBoolean(key, value).apply()
    }

    fun getBoolean(key: String, defaultValue: Boolean = false): Boolean {
        checkInitialization()
        return sharedPreferences.getBoolean(key, defaultValue)
    }

    fun putInt(key: String, value: Int) {
        checkInitialization()
        sharedPreferences.edit().putInt(key, value).apply()
    }

    fun getInt(key: String, defaultValue: Int = 0): Int {
        checkInitialization()
        return sharedPreferences.getInt(key, defaultValue)
    }

    fun putLong(key: String, value: Long) {
        checkInitialization()
        sharedPreferences.edit().putLong(key, value).apply()
    }

    fun getLong(key: String, defaultValue: Long = 0L): Long {
        checkInitialization()
        return sharedPreferences.getLong(key, defaultValue)
    }

    fun putFloat(key: String, value: Float) {
        checkInitialization()
        sharedPreferences.edit().putFloat(key, value).apply()
    }

    fun getFloat(key: String, defaultValue: Float = 0f): Float {
        checkInitialization()
        return sharedPreferences.getFloat(key, defaultValue)
    }

    fun putString(key: String, value: String) {
        checkInitialization()
        sharedPreferences.edit().putString(key, value).apply()
    }

    fun getString(key: String, defaultValue: String = ""): String? {
        checkInitialization()
        return sharedPreferences.getString(key, defaultValue)
    }

    fun putStringSet(key: String, value: Set<String>) {
        checkInitialization()
        sharedPreferences.edit().putStringSet(key, value).apply()
    }

    fun getStringSet(key: String, defaultValue: Set<String> = emptySet()): Set<String>? {
        checkInitialization()
        return sharedPreferences.getStringSet(key, defaultValue)
    }

    fun remove(key: String) {
        checkInitialization()
        sharedPreferences.edit().remove(key).apply()
    }

    fun clear() {
        checkInitialization()
        sharedPreferences.edit().clear().apply()
    }

    private fun checkInitialization() {
        if (!::sharedPreferences.isInitialized) {
            throw IllegalStateException("SharedPreferencesUtils is not initialized. Call init() first.")
        }
    }
}